package com.gxc.authcore.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.authcore.system.domain.User;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.CompareType;
import com.gxc.dbcore.util.ConditionUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.StringUtil;

@Service
public class UserService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;

	public User retrieve(String id) {
		return beanService.get(sqlDao, User.class, id);
	}

	public int create(User user) {
		return beanService.create(sqlDao, user);
	}

	public int update(User user) {
		return beanService.update(sqlDao, user);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, User.class, id);
	}

	public int delete(String[] idArray) {
		return beanService.delete(sqlDao, User.class, idArray);
	}

	@SuppressWarnings("rawtypes")
	public List<CompareItem> buildConditions(Map params) {
		List<CompareItem> conditionList = new ArrayList<CompareItem>();
		conditionList.add(new CompareItem("id", CompareType.EQUAL));
		conditionList.add(new CompareItem("code", CompareType.EQUAL));
		conditionList.add(new CompareItem("name", CompareType.LIKE));
		conditionList.add(new CompareItem("email", CompareType.LIKE));
		conditionList.add(new CompareItem("mobile", CompareType.LIKE));
		conditionList.add(new CompareItem("note", CompareType.LIKE));
		conditionList = ConditionUtil.setConditionValue(conditionList, params);
		return conditionList;
	}

	@SuppressWarnings("rawtypes")
	public List list(Map params) {
		List<CompareItem> conditionList = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.list(sqlDao, User.class, conditionList, fieldSort);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map params, PageInfo pageInfo) {
		List<CompareItem> conditionList = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.listPage(sqlDao, User.class, conditionList,
				fieldSort, pageInfo);
	}

	/**
	 * 获取user关联的role
	 * 
	 * @param parameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList listPageOfRole(Map parameter, PageInfo pageInfo) {
		return sqlDao.selectPageList("system.user.listOfRole", parameter,
				pageInfo);
	}

	@SuppressWarnings("rawtypes")
	public List listRelatedRole(Map parameter) {
		return sqlDao.selectList("system.user.listRelatedRole", parameter);
	}

	/**
	 * 添加user与role的关联
	 * 
	 * @param role_id
	 * @param action_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int createRoleRelate(String user_id, String role_id) {
		HashMap params = new HashMap();
		params.put("id", StringUtil.getUUID());
		params.put("user_id", user_id);
		params.put("role_id", role_id);
		return sqlDao.insert("system.user.createRoleRelate", params);
	}

	/**
	 * 删除user与role的关联
	 * 
	 * @param role_id
	 * @param action_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int deleteRoleRelate(String user_id, String role_id) {
		HashMap params = new HashMap();
		params.put("user_id", user_id);
		params.put("role_id", role_id);
		return sqlDao.delete("system.user.deleteRoleRelate", params);
	}
}
